﻿// $Id: CompositeController.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 NourY Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*! \file
 * \brief Definition of \c Salamanca.DataActivities.CompositeController.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */

using System;
using System.Collections.Generic;
using System.Text;
using Salamanca.DataRules;

namespace Salamanca.DataActivities.UI
{

    internal class CompositeController:
        IActivityController
    {

        /// <summary>Constructor.</summary>
        /// <param name="compositeState">The composite activity state.</param>
        public CompositeController(IActivityState compositeState)
        {
            _State=compositeState;
        }

        /// <summary>Executes the activity.</summary>
        public ActivityStateResult Execute()
        {
            ActivityStateResult ret=ActivityStateResult.Stop;
            do
            {
                ret=DoExecute();
            } while (ActivityResult(ret));
            return ret;
        }

        /// <summary>Executes the activity.</summary>
        public ActivityStateResult Execute(ActivityStateResult result)
        {
            if (ActivityResult(result))
                return Execute();
            else
                return result;
        }

        /// <summary>Processes the current state.</summary>
        private ActivityStateResult DoExecute()
        {
            _State.Initialize(this);
            ActivityStateResult ret=_State.Handle(this);
            _State.Terminate(this);

            return ret;
        }

        private bool ActivityResult(ActivityStateResult result)
        {
            return false;
        }

        /// <summary>Marks the current activity state for saving.</summary>
        public void MarkupState()
        {
            throw new NotSupportedException();
        }

        /// <summary>The current activity state.</summary>
        public IActivityState CurrentState
        {
            get
            {
                return _State;
            }
        }

        private IActivityState _State;

    }
}
